const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoadData: false,
    orderItem: [], //订单列表
    isHints: false,
    pageIndex: 1,
    pageSize: 20,
    pageCount: 1,
    loadText: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserOrderDetail();
  },

  /**
   * 滚动到底部/右边时触发
   */
  scrollEnd: function() {
    this.setData({
      pageIndex: this.data.pageIndex + 1,
      isLoadData: true,
      loadText: "加载中..."
    }, function() {
      if (this.data.pageIndex > this.data.pageCount) {
        this.setData({
          isLoadData: true,
          loadText: "已经到底了"
        });
      } else {
        this.getUserOrderDetail();
      }
    })
  },

  /**
   * 订单接口
   */
  getUserOrderDetail: function() {
    var param = {
      merchantId: wx.getStorageSync('merchantId'),
      "offset": (this.data.pageIndex - 1) * this.data.pageSize,
      "limit": this.data.pageSize
    }
    memberModel.userOrderDetail(param).then(data => {
      if (data) {
        let pageCountTotal = data.totalCount % this.data.pageSize == 0 ? (data.totalCount / this.data.pageSize) : (parseInt(data.totalCount / this.data.pageSize) + 1);
        this.setData({
          pageCount: pageCountTotal
        })
        if (data.totalCount > 0) {
          var orderData = [];
          for (var i = 0; i < data.data.length; i++) {
            var item = data.data[i];
            var list = {};
            var orderType = "";
            if (item.type == 1) {
              orderType = item.orderStatus == 0 ? '待付款' : item.orderStatus == 1 ? '待发货' : item.orderStatus == 2 ? '待收货' : item.orderStatus == 3 ? '已完成' : item.orderStatus == 4 ? '已取消' : '';
            } else if (item.type == 2) {
              orderType = item.orderStatus == 1 ? '退款成功' : (item.orderStatus == 2 || item.orderStatus == 0) ? '退款中' : '';
            }
            list.type = item.type; //订单类型 正向订单:1 退单:2
            list.orderCode = item.orderCode; //订单编号
            list.orderName = item.orderName; //订单名称
            list.orderStatus = item.orderStatus; //订单状态 正向 0:待付款 1:待发货 2:待收货 3:已完成 4: 已取消 逆向 1:退款成功 2或0: 退款中
            list.totalFee = item.totalFee; //订单金额
            list.promotionDiscount = item.promotionDiscount; //抵扣金额
            list.payableFee = item.payableFee; //应付金额（消费金额）
            list.totalQty = item.totalQty; //商品总数量
            list.orderType = orderType; //订单类型
            list.itemsOrder = item.items;//订单套餐商品详情
            list.createTime = item.createTime ? item.createTime.replace(/-/g, "/") : ""; //时间
            orderData.push(list);
          };
          // 将新一页的数据添加到原数据后面
          var orderDataList = this.data.orderItem;
          var newOrderData = orderDataList.concat(orderData);
          this.setData({
            isLoadData: false,
            isHints: false,
            orderItem: newOrderData
          })
        } else {
          this.setData({
            loadText: "",
            isLoadData: false,
            isHints: true,
            orderItem: []
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

})